#encoding:utf-8
import os

def datasetCreate():
    items=[]#总共45个用例涉及4822个对象
    files=os.listdir('E:\\Development\\TracesFrequency\\')
    for file in files:
        f=open('E:\\Development\\TracesFrequency\\'+file)
        for line in f:
            temp=line.split(',')
            if temp[1].strip() not in items:
                items.append(temp[1].strip())
        f.close()
    title=','.join(items)
    title=title+'\n'

    dic={}
    result=''
    sum=0
    for file in files:
        f=open('E:\\Development\\TracesFrequency\\'+file)
        for line in f:
            temp=line.split(',')
            if temp[1].strip() in items:#为每一个用例计算出现数
                result=result+temp[0].strip()+','
            else:
                result=result+'0'+','
                
            if dic.has_key(temp[0].strip()):# 统计出现数出现的频率，字典dic的格式： dic{string:int}
                dic[temp[0].strip()]=dic[temp[0].strip()]+1
            else:
                dic[temp[0].strip()]=1
            sum+=int(temp[0].strip())
                
        result=result.rstrip(',')
        result=result+'\n'
        
        f.close()
    
    dataset=open('E:\\Development\\dataset.txt','w')
    dataset.write(title)
    dataset.write(result)
    dataset.close()

    s=''
    for j in dic.keys():
        s=s+j+':'+str(dic[j])+'\n'
    ocf=open('E:\\Development\\OccNoMapping.txt','w')
    ocf.write(s)
    ocf.close()
    
        
    titleMap={}
    j=0
    for t in items:
        titleMap[t]=j
        j=j+1

    return dic,sum,titleMap

def Discretization():# 对出现数进行离散化处理
    dic,sum,firstLineMap=datasetCreate()
    key=dic.keys()
    key.sort()
    temp=key
    stage=sum/10.0  

    total=0
    width={}
    index=0
    for i in range(10):
        w=[]
        for k in key:
            total=total+(int(k)*dic[k])
            if total>(i+1)*stage:
                total=total-(int(k)*dic[k])
                break
            else:
                w.append(k)
                index=index+1
        width[i]=w
        key=temp[index:]
    return width,firstLineMap


def Result():
    SplitSpace,tm=Discretization()
    files=os.listdir('E:\\Development\\TracesFrequency\\')
    for file in files:
        f=open('E:\\Development\\TracesFrequency\\'+file)
        re=''
        key=0
        value=0
        for line in f:
            dt=line.split(',')
            if dt[1].strip() in tm.keys():
                key=tm[dt[1].strip()]#对象名索引
            for i in range(10):
                if dt[0].strip() in SplitSpace[i]:
                    value=i# 将出现数映射为不同的区间
                
           
            re=re+str(key)+','+str(value)+'\n'
            
            
        newf=open('E:\\Development\\result\\'+file,'w')
        newf.write(re)#object name index+occurence number

        f.close()
        newf.close()
        